<?php

class IndexController extends Core_Controller_Action 
{

	public function init() {
	}
	
	public function authAction() {
		$this->view->title = 'login';
		$this->view->error = false;
		if( $this->_request->isPost() ) {
			$adapter = new Core_Auth_Adapter($this->_getParam('email'), md5( $this->_getParam('password') ) );
	        $result = Zend_Auth::getInstance()->authenticate($adapter);
	        if( Zend_Auth::getInstance()->hasIdentity() ) {
	        	$this->_redirect( 'basket' );
	        }
	        $this->view->error = true;
		}
	}

	public function indexAction () {
		echo $this->userData->role;
		$this->view->title = 'main';
		
		$prod_api = new Products();
		$this->view->products_list_lay = $prod_api-> getListWithName($this->locale)->toArray();
		
		$image = new Gallery();
		$this->view->first_gal = $image->getGalByProd($this->view->products_list_lay[0]['p_id']);
	}
	
	public function newsletterAction() {
	
		$this->_helper->layout->disableLayout();
		$this->_helper->viewRenderer->setNoRender();
	
		$nlTable = Doctrine_Core::getTable('Newsletter');
		$it = $nlTable->findOneByEmail( $this->_request->getPost( 'email' ) );
		if( $it ) {
			echo Zend_Json::encode(array(
				'status' => false
			)); 
		} else {
			$Newsletter = new Newsletter();
			$Newsletter->email = $this->_request->getPost( 'email' );
			$Newsletter->save();

			echo Zend_Json::encode(array(
				'status' => true
			));
		}
	}
	
	public function newslettersendAction() {
	
		$this->_helper->layout->disableLayout();
		$this->_helper->viewRenderer->setNoRender();
	
		$Nq = new NewsletterQueue();
		$it = $Nq->getItem();
		
		if( $it ) {
		
			$it = $it->getFirst();
			
			$N = new Newsletter();
			$list = $N->getList( intval( $it['last_id'] ) );
			$list = $list->toArray();
			if( $list ) {
				foreach( $list as $val ) {
					
					if( filter_var( $val['email'], FILTER_VALIDATE_EMAIL ) ) {
						
					}
					
				}
				
				$it->last_id = $val['n_id'];
				$it->save();
			} else {
				$Nq->deleteItem( $it['nq_id'] );
			}
		
		}
	}
	
	
	public function showoneAction () {
		$gamename= $this->_request->getParam( 'gamename' );
		$games = new Products();	
		$this->view->game = $games->getByUrl($gamename, $this->locale)->toArray();
		$this->view->show_title = true;
		$this->view->title = $this->view->game['title'];
		
		$gal = new Gallery();
		$this->view->gal = $gal->getListNoMain( $this->view->game['p_id'] )->toArray();
		
		$cat= new DescriptionCategory();
		$this->view->cat = $cat->getItemByCategoryAndLang( $this->view->game['category_c_id'],$this->locale )->getFirst()->toArray();
	}
	
	public function logoutAction() {
		$this->_helper->layout->disableLayout();
		$this->_helper->viewRenderer->setNoRender();
		
    	Zend_Auth::getInstance()->clearIdentity();
    	
    	$sess = new Zend_Session_Namespace( 'ranks' );
    	$sess->unsetAll();
    	
    	$nored = $this->_request->getParam( 'nored', 0 );
    	
	    	if( !$nored ) {
	    		$this->_redirect('/index');
	    	} else {
	    		echo 'result=1';
	    	}
	}
	public function productsAction(){
		
		$gamename= $this->_request->getParam( 'gamename' );
		$games = new Category();	
		$this->view->game = $games->getByUrl($gamename, $this->locale)->toArray();
		$this->view->show_title = true;
		$this->view->title = $this->view->game['title'];
		
		$prod_api = new Products();
		$page = intval( $this->_request->getParam( 'page', 1 ) );
		$pagerLayout= $prod_api->getProduktytWithPager($this->view->game['c_id'],$page, $this->locale);
		
		$this->view->products_list_cat = $pagerLayout->execute();
		$this->view->pager = $pagerLayout;
	}
	public function helpAction(){
		$this->view->title = 'help';
		
	}
	public function aboutAction(){
		$this->view->title = 'about';
		
	}
	public function remindAction(){
		$this->view->title = 'forgot';
		$this->view->error = false;
		if( $this->_request->isPost() ) {
			$user = Doctrine_Core::getTable('Users')->findOneByEmail($this->_request->getParam( 'email' ));
			$pass = md5(rand());
			$user->pass = md5($pass);
			$user->save();
			
			$mail = new Core_Mail();
			$mail->addTo($user->email, $user->first_name.' '.$user->last_name );
			$mail->setFrom( 'no-reply@airblaster.pl', 'Airblaster.pl' );
			$mail->setSubject( "Przypomnienei hasła");
			$mail->setBodyHtml( "Witaj, Twoje nowe hasło to ". $pass );
			$mail->send();
			
	        $this->view->error = true;
		}
	}
	public function registrationAction(){
		$this->view->title = 'reg';
		$this->view->error = false;
		if( $this->_request->isPost() ) {
			$user = new Users();
			$user->email = $this->_request->getParam( 'email' );
			$user->pass = md5($this->_request->getParam( 'password' ));
			$user->save();
			
			$mail = new Core_Mail();
			$mail->addTo($user->email, $user->email );
			$mail->setFrom( 'no-reply@airblaster.pl', 'Airblaster.pl' );
			$mail->setSubject( "Założyłeś konto");
			$mail->setBodyHtml( "Witaj, właśnie założyółeś konto. login: ".$user->email." hasło: ".$this->_request->getParam( 'password' ) );
			$mail->send();
	        $this->view->error = true;
		}
	}
}
